package cn.ming.wfchat.app.me.fragemt;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.ming.wfchat.R;
import cn.ming.wfchat.app.AppService;
import cn.ming.wfchat.app.me.activity.model.AssetsBean;
import cn.ming.wfchat.app.me.adpter.AssetsListAdapter;
import cn.wildfirechat.remote.ChatManager;

/**
 * @author luoming
 * created at 2019-10-14 10:32
 * 币种Usdt
 */
public class CurrencyUsdtFragemt extends Fragment {

    @BindView(R.id.tv_sumBalance)
    TextView tvSumBalance;
    @BindView(R.id.tv_usdtRate)
    TextView tvUsdtRate;
    @BindView(R.id.tv_usdtRate_text)
    TextView tvUsdtRateText;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    private AssetsListAdapter assetsListAdapter;

    public static CurrencyUsdtFragemt newInstance() {
        CurrencyUsdtFragemt situationFragemt = new CurrencyUsdtFragemt();
        Bundle bundle = new Bundle();
        situationFragemt.setArguments(bundle);
        return situationFragemt;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.currency_usdt_fragemt, container, false);
        ButterKnife.bind(this, view);
        assetsListAdapter = new AssetsListAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(assetsListAdapter);
        ShowData();
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    public void ShowData() {
        SharedPreferences mSavePreferencesData = getActivity().getSharedPreferences("config", Context.MODE_PRIVATE);
        String findMyAssets = mSavePreferencesData.getString("findMyAssets", null);
        if (findMyAssets != null) {
            AssetsBean assetsBean = JSON.parseObject(findMyAssets, AssetsBean.class);
            initView(assetsBean);
        }
        AppService.Instance().findMyAssets(new AppService.AssetsCCallback() {
            @Override
            public void onUiSuccess(AssetsBean assetsBean) {
                initView(assetsBean);
                mSavePreferencesData.edit().putString("findMyAssets", JSON.toJSONString(assetsBean));
            }

            @Override
            public void onUiFailure(int code, String msg) {
                ChatManager.showToast(msg);

            }
        });

    }

    void initView(AssetsBean assetsBean) {
        tvUsdtRateText.setText("实时指数：USDT/CNY = " + assetsBean.getUsdtRate());
        tvSumBalance.setText(assetsBean.getSumAmount().toPlainString() + "CNY");
        assetsListAdapter.setNewData(assetsBean.getList());
    }

}
